﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CueMounter {
	public partial class AuCDTectForm : Form {
		private const string StatusPending = "Pending";
		private const string StatusProcessing = "Processing";
		private const string StatusCompleted = "Completed";
		private const string StatusCancelled = "Cancelled";

		AuCDTectProcess[] Processes;
		int currentItem = 0;
		bool shouldCancel = false, shouldClose = false;

		public AuCDTectForm(string[] files) {
			InitializeComponent();

			Processes = new AuCDTectProcess[Environment.ProcessorCount];

			foreach(string file in files) {
				ListViewItem lvi = new ListViewItem();
				lvi.SubItems[0].Text = file;
				lvi.SubItems.Add(StatusPending);
				lvi.SubItems.Add("");
				listTracks.Items.Add(lvi);
			}

			LaunchProcesses();
		}

		private void OnCompleted() {
			btnCancelClose.Text = "Close";
			for(int i = currentItem; i < listTracks.Items.Count; ++i) {
				listTracks.Items[i].SubItems[1].Text = StatusCancelled;
			}
		}

		public void LaunchProcesses() {
			for(int i = 0; i < Processes.Length; ++i) {
				if(currentItem == listTracks.Items.Count) {
					break;
				}
				if(Processes[i] == null) {
					Processes[i] = new AuCDTectProcess(listTracks.Items[currentItem].SubItems[0].Text);
					listTracks.Items[currentItem].SubItems[1].Text = StatusProcessing;
					Processes[i].Completed += new EventHandler<AuCDTectCompleteEventArgs>(Process_Completed);
					Processes[i].Start();

					++currentItem;
					if(currentItem == listTracks.Items.Count) {
						break;
					}
				}
			}
		}

		void Process_Completed(object sender, AuCDTectCompleteEventArgs e) {
			AuCDTectProcess process = (AuCDTectProcess)sender;
			Invoke(new Action(delegate() {
				for(int i = 0; i < Processes.Length; ++i) {
					if(Processes[i] == process) {
						Processes[i] = null;
					}
				}
				for(int i = 0; i < listTracks.Items.Count; ++i) {
					if(listTracks.Items[i].SubItems[0].Text == process.File) {
						listTracks.Items[i].SubItems[1].Text = StatusCompleted;
						listTracks.Items[i].SubItems[2].Text = e.Type + " with probability " + e.Probability + "%";
					}
				}
				if(shouldCancel) {
					if(shouldClose) {
						Close();
					}
				} else {
					LaunchProcesses();
				}
				if(!IsStillRunning) {
					OnCompleted();
				}
			}));
		}

		private bool IsStillRunning {
			get {
				for(int i = 0; i < Processes.Length; ++i) {
					if(Processes[i] != null) {
						return true;
					}
				}
				return false;
			}
		}

		private void AuCDTectForm_FormClosing(object sender, FormClosingEventArgs e) {
			if(IsStillRunning) {
				shouldCancel = true;
				shouldClose = true;
				e.Cancel = true;
			}
		}

		private void btnCancelClose_Click(object sender, EventArgs e) {
			if(IsStillRunning) {
				shouldCancel = true;
			} else {
				Close();
			}
		}
	}
}
